<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商铺编号" prop="shopsId">
        <el-select v-model="queryParams.shopsId" placeholder="请选择" filterable clearable
            :style="{width: '100%'}" @keyup.enter.native="handleQuery">
            <el-option v-for="(item, index) in rentalcontractList" :key="index" :label="item.shopsCode"
              :value="item.shopsId" :disabled="item.disabled"></el-option>
        </el-select>

      </el-form-item>

     <!-- <el-form-item label="收费项目名" prop="chargeitemId">
        <el-select v-model="queryParams.chargeitemId" placeholder="请选择" filterable clearable
            :style="{width: '100%'}" @keyup.enter.native="handleQuery">
            <el-option v-for="(item, index) in chargeitemList" :key="index" :label="item.name"
              :value="item.id" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item> -->
 <!--     <el-form-item label="缴费单号" prop="billNumber">
        <el-input
          v-model="queryParams.billNumber"
          placeholder="请输入缴费单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="支付状态" prop="paymentState" label-width="95px">
        <el-select v-model="queryParams.paymentState" placeholder="请选择支付状态" clearable>
          <el-option
            v-for="dict in dict.type.zhwy_chargedata_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="单价" prop="univalence">
        <el-input
          v-model="queryParams.univalence"
          placeholder="请输入单价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="费用开始时间" prop="startTime" label-width="120px">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择费用开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="费用结束时间" prop="endTime" label-width="120px">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择费用结束时间">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="催缴期限(如果不缴费，则状态改为逾期)" prop="deadlineTime">
        <el-date-picker clearable
          v-model="queryParams.deadlineTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择催缴期限(如果不缴费，则状态改为逾期)">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="上次读数" prop="lastData">
        <el-input
          v-model="queryParams.lastData"
          placeholder="请输入上次读数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="本次读数" prop="thisData">
        <el-input
          v-model="queryParams.thisData"
          placeholder="请输入本次读数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="倍率【1】" prop="rate">
        <el-input
          v-model="queryParams.rate"
          placeholder="请输入倍率【1】"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="损耗【0】" prop="wastage">
        <el-input
          v-model="queryParams.wastage"
          placeholder="请输入损耗【0】"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量【" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入数量【"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="滞纳金【如果当前收费项有滞纳金，则产生[总金额 * 滞纳金百分比 * 滞纳天数]】" prop="latefee">
        <el-input
          v-model="queryParams.latefee"
          placeholder="请输入滞纳金【如果当前收费项有滞纳金，则产生[总金额 * 滞纳金百分比 * 滞纳天数]】"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="金额【数量*单价】" prop="money">
        <el-input
          v-model="queryParams.money"
          placeholder="请输入金额【数量*单价】"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实收金额" prop="fundsReceived">
        <el-input
          v-model="queryParams.fundsReceived"
          placeholder="请输入实收金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->

      <!-- <el-form-item label="退款状态【数据字典】" prop="refundStatus">
        <el-select v-model="queryParams.refundStatus" placeholder="请选择退款状态【数据字典】" clearable>
          <el-option
            v-for="dict in dict.type.zhwy_chargedata_refundstatus"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="退款总金额" prop="totalRefundAmount">
        <el-input
          v-model="queryParams.totalRefundAmount"
          placeholder="请输入退款总金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->

      <el-form-item label="缴费时间" prop="chargeTime">
        <el-date-picker clearable
          v-model="queryParams.chargeTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择缴费时间">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="缴费方式id,关联支付方式表" prop="payId">
        <el-input
          v-model="queryParams.payId"
          placeholder="请输入缴费方式id,关联支付方式表"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收款人姓名" prop="chargeUser">
        <el-input
          v-model="queryParams.chargeUser"
          placeholder="请输入收款人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"

          @click="handleAdd"
          v-hasPermi="['feeSettings:chargedata:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd1"
          v-hasPermi="['feeSettings:chargedata:add']"
        >批量新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd2"
          v-hasPermi="['feeSettings:chargedata:add']"
        >生成数据(非抄表)</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd3"
          v-hasPermi="['feeSettings:chargedata:add']"
        >生成抄表数据</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['feeSettings:chargedata:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['feeSettings:chargedata:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['feeSettings:chargedata:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="chargedataList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="主键，自增" align="center" prop="id" /> -->
      <el-table-column label="序号" align="center" type="index" width="50" />
      <el-table-column label="商铺编号" align="center" prop="shopsCode" />
      <el-table-column label="收费项目" align="center" prop="chargeitemName" />
      <el-table-column label="客户姓名" align="center" prop="hirerName" />
      <el-table-column label="费用开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="费用结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="催缴期限(如果不缴费，则状态改为逾期)" align="center" prop="deadlineTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deadlineTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="上次读数" align="center" prop="lastData" />
      <el-table-column label="本次读数" align="center" prop="thisData" />
      <el-table-column label="倍率" align="center" prop="rate" />
      <el-table-column label="损耗" align="center" prop="wastage" />
      <el-table-column label="数量" align="center" prop="number" />
      <!-- <el-table-column label="滞纳金【如果当前收费项有滞纳金，则产生[总金额 * 滞纳金百分比 * 滞纳天数]】" align="center" prop="latefee" /> -->
      <el-table-column label="单价" align="center" prop="univalence" />
      <el-table-column label="金额" align="center" prop="money" />
      <el-table-column label="实收金额" align="center" prop="fundsReceived" />
      <el-table-column label="付款状态" align="center" prop="paymentState">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.zhwy_chargedata_state" :value="scope.row.paymentState"/>
        </template> -->
        <template slot-scope="scope">
          <el-tag v-if="scope.row.paymentState===1">已付款</el-tag>
          <el-tag type="danger" v-else>未付款</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="退款状态" align="center" prop="refundStatus">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.zhwy_chargedata_refundstatus" :value="scope.row.refundStatus"/>
        </template> -->
        <template slot-scope="scope">
          <el-tag v-if="scope.row.refundStatus===0">未退款</el-tag>
          <el-tag type="danger" v-if="scope.row.refundStatus===1">已退款</el-tag>
          <el-tag type="danger" v-if="scope.row.refundStatus===2">部分退款</el-tag>
          <!-- <el-tag type="danger" v-else>未付款</el-tag> -->
        </template>
      </el-table-column>
      <el-table-column label="退款总金额" align="center" prop="totalRefundAmount" />
      <el-table-column label="缴费单号" align="center" prop="billNumber" />
      <el-table-column label="缴费时间" align="center" prop="chargeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.chargeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="缴费方式id,关联支付方式表" align="center" prop="payId" />
      <el-table-column label="收款人姓名" align="center" prop="chargeUser" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            :disabled="scope.row.paymentState == 1"
            v-hasPermi="['feeSettings:chargedata:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            :disabled="scope.row.paymentState == 1"
            v-hasPermi="['feeSettings:chargedata:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleUpdate2(scope.row)"
            :disabled="scope.row.paymentState == 0"
            v-hasPermi="['feeSettings:chargedata:edit']"
          >退款</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改收费数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="商铺编号" prop="shopsId" >
          <!-- <el-input v-model="form.shopsId" placeholder="请输入商铺id，外键"  style="width: 76%;"/> -->
          <el-select v-model="form.shopsId" placeholder="请选择" filterable clearable
            :style="{width: '76%'}" @keyup.enter.native="handleQuery" >
            <el-option v-for="(item, index) in rentalcontractList" :key="index" :label="item.shopsCode"
              :value="item.shopsId" :disabled="item.disabled"></el-option>
        </el-select>
          <el-button  icon="el-icon-search" @click="kehu()" type="primary">搜索</el-button>
        </el-form-item>
        <el-form-item label="客户姓名" prop="hirerName">
          <el-input :style="{width: '95%'}" v-model="hirerName" placeholder="请输入客户姓名" disabled/>
        </el-form-item>
        <el-form-item label="收费项目名" prop="chargeitemId">
          <el-select v-model="form.chargeitemId" placeholder="请选择" filterable clearable
            :style="{width: '95%'}" @keyup.enter.native="handleQuery" @change="wathcdanjia()" >
            <el-option v-for="(item, index) in chargeitemList" :key="index" :label="item.name"
              :value="item.id" :disabled="item.disabled"></el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="单价" prop="univalence">
          <el-input :style="{width: '95%'}" v-model="form.univalence" placeholder="请输入单价" disabled/>
        </el-form-item>
        <el-form-item label="费用开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择费用开始时间" :style="{width: '95%'}">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="费用结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择费用结束时间" :style="{width: '95%'}">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="催缴期限" prop="deadlineTime">
          <el-date-picker clearable
            v-model="form.deadlineTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择催缴期限" :style="{width: '95%'}">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上次读数" prop="lastData">
          <el-input :style="{width: '95%'}" v-model="form.lastData" placeholder="请输入上次读数" />
        </el-form-item>
        <el-form-item label="本次读数" prop="thisData">
          <el-input :style="{width: '95%'}" v-model="form.thisData" placeholder="请输入本次读数" />
        </el-form-item>
        <el-form-item label="倍率" prop="rate">
          <el-input :style="{width: '95%'}" v-model="form.rate" placeholder="请输入倍率" />
        </el-form-item>
        <el-form-item label="损耗" prop="wastage">
          <el-input :style="{width: '95%'}" v-model="form.wastage" placeholder="请输入损耗" />
        </el-form-item>
        <el-form-item label="数量" prop="number">
          <el-input :style="{width: '95%'}" v-model="form.number" placeholder="请输入数量" disabled/>
        </el-form-item>
        <el-form-item label="金额" prop="money">
          <el-input :style="{width: '95%'}" v-model="form.money" placeholder="请输入金额" disabled/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!-- 退款 -->
     <el-dialog :title="title" :visible.sync="opentuifei" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择费用开始时间" :style="{width: '95%'}" disabled>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实收金额" prop="fundsReceived">
          <el-input :style="{width: '95%'}" v-model="form.fundsReceived" placeholder="请输入实收金额" disabled/>
        </el-form-item>
        <el-form-item label="退款总金额" prop="totalRefundAmount">
          <el-input :style="{width: '95%'}" v-model="form.totalRefundAmount" placeholder="请输入退款总金额" />
        </el-form-item>
        <!-- <el-form-item label="退款状态" prop="refundStatus">
          <el-select  v-model="form.refundStatus" placeholder="请选择退款状态">
            <el-option
              v-for="dict in dict.type.zhwy_chargedata_refundstatus"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormtuikuan">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



<!-- 添加或修改收费数据 批量新增 -----测试-->
<el-dialog :title="title" :visible.sync="open1" width="600px" append-to-body>
        <el-steps :active="active" align-center>
        <el-step title="新增规则" ></el-step>
        <el-step title="数据确认" ></el-step>
        </el-steps>
      <el-form v-if="this.active===1" ref="form" :model="form" :rules="rules" label-width="120px" style="padding:20px">
        <el-form-item label="商铺编号" prop="shopsId" >
          <el-input v-model="form.shopsId" placeholder="请输入商铺id，外键"  style="width: 80%;"/>
          <!-- <el-select v-model="form.shopsId" placeholder="请选择" filterable clearable
            :style="{width: '76%'}" @keyup.enter.native="handleQuery" >
            <el-option v-for="(item, index) in rentalcontractList" :key="index" :label="item.shopsCode"
              :value="item.shopsId" :disabled="item.disabled"></el-option>
        </el-select> -->
          <!-- <el-button  icon="el-icon-search" @click="kehu()" type="primary">搜索</el-button> -->
        </el-form-item>
        <!-- <el-form-item label="客户姓名" prop="hirerName">
          <el-input v-model="hirerName" placeholder="请输入客户姓名" disabled/>
        </el-form-item> -->
        <el-form-item label="收费项目名" prop="chargeitemId">
          <el-select v-model="form.chargeitemId" placeholder="请选择" filterable clearable
            :style="{width: '80%'}" @keyup.enter.native="handleQuery" @change="wathcdanjia()">
            <el-option v-for="(item, index) in chargeitemList" :key="index" :label="item.name"
              :value="item.id" :disabled="item.disabled"></el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="单价" prop="univalence">
          <el-input :style="{width: '80%'}" v-model="form.univalence" placeholder="请输入单价" disabled/>
        </el-form-item>
        <el-form-item label="费用开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择费用开始时间" :style="{width: '80%'}">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="费用结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择费用结束时间" :style="{width: '80%'}">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="催缴期限" prop="deadlineTime">
          <el-date-picker clearable
            v-model="form.deadlineTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择催缴期限" :style="{width: '80%'}">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <el-form v-if="this.active===2" ref="form" :model="form" :rules="rules" label-width="120px" style="padding:20px">
        <el-table :data="form.shops" style="width: 100%">
          <el-table-column label="序号" align="center" type="index" />
          <el-table-column label="商铺编号" align="center" prop="shopsId">
            <!-- <template >
              <span>{{ form.shopsId }}</span>
            </template> -->
          </el-table-column>
          <el-table-column label="收费项名" align="center" prop="chargeitemName">
            <!-- <template >
              <span>{{ form.chargeitemId }}</span>
            </template> -->
          </el-table-column>
          <el-table-column label="客户姓名" align="center" prop="hirerName" >
            <!-- <template >
              <span>{{ hirerId }}</span>
            </template> -->
          </el-table-column>
          <!-- <el-table-column label="数量" align="center" prop="number" >
          <template >
              <span>{{ 1 }}</span>
            </template>
          </el-table-column> -->
          <el-table-column label="单价" align="center" prop="univalence" >
          <!-- <template >
              <span>{{ form.univalence }}</span>
            </template> -->
          </el-table-column>
          <el-table-column label="金额" align="center" prop="univalence" >
          <!-- <template >
              <span>{{ form.money }}</span>
            </template> -->
          </el-table-column>
          <el-table-column label="费用开始时间" align="center" prop="startTime" >
            <!-- <template >
              <span>{{ form.startTime }}</span>
            </template> -->
          </el-table-column>
          <el-table-column label="费用结束时间" align="center" prop="endTime" >
            <!-- <template >
              <span>{{ form.endTime }}</span>
            </template> -->
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="this.active!==1" style="margin-top: 12px;" @click="back">上一步</el-button>
        <el-button v-if="this.active!==2" style="margin-top: 12px;" @click="next">下一步</el-button>
        <el-button style="margin-top: 12px;" @click="cancel">取消</el-button>
        <el-button v-if="active === 2" style="margin-top: 12px;" @click="addList">确定</el-button>
      </div>
    </el-dialog>








    <el-dialog :title="title" :visible.sync="open2" width="600px" append-to-body>
        <el-steps :active="active" align-center>
        <el-step title="生成规则" ></el-step>
        </el-steps>
      <el-form v-if="this.active==1" ref="form" :model="form" :rules="rules" label-width="120px" style="padding:20px">
        <el-form-item label="收费项目名" prop="chargeitemId">
          <el-select v-model="form.chargeitemId" placeholder="请选择" filterable clearable
            :style="{width: '100%'}" @keyup.enter.native="handleQuery" @change="wathcdanjia()">
            <el-option v-for="(item, index) in chargeitemList" :key="index" :label="item.name"
              :value="item.id" :disabled="item.disabled"></el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="单价" prop="univalence">
          <el-input v-model="form.univalence" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="费用开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择费用开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="费用结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择费用结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="催缴期限" prop="deadlineTime">
          <el-date-picker clearable
            v-model="form.deadlineTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择催缴期限(如果不缴费，则状态改为逾期)">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button style="margin-top: 12px;" @click="cancel">取消</el-button>
        <el-button  style="margin-top: 12px;" @click="cancel">生成数据</el-button>
      </div>
    </el-dialog>


        <el-dialog :title="title" :visible.sync="open3" width="600px" append-to-body>
        <el-steps :active="active" align-center>
        <el-step title="生成规则" ></el-step>
        </el-steps>
      
        <el-form v-if="this.active==1" :model="queryParams4"  ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="180px" style="padding:20px">
          
        <el-form-item label="收费项目名" prop="chargeitemId">
          <el-select v-model="queryParams4.chargeitemId" placeholder="请选择"  
            :style="{width: '177px'}" @keyup.enter.native="handleQuery"  >
            <el-option v-for="(item, index) in chargeitemList" :key="index" :label="item.name"
              :value="item.id" :disabled="item.disabled"></el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="费用开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="queryParams4.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择费用开始时间" :style="{width: '80%'}">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="费用结束时间" prop="endTime" >
          <el-date-picker clearable
            v-model="queryParams4.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择费用结束时间" :style="{width: '80%'}">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="催缴期限" prop="deadlineTime">
          <el-date-picker clearable
            v-model="form.deadlineTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择催缴期限(如果不缴费，则状态改为逾期)">
          </el-date-picker>
        </el-form-item> -->
        
      </el-form>
      <el-form v-if="this.active===2" ref="form" :model="form" :rules="rules" label-width="120px" style="padding:20px">
        <el-table :data="chaobiaolist" style="width: 100%">
          <el-table-column label="序号" align="center" type="index" />
          <el-table-column label="商铺编号" align="center" prop="shopsCode">
          </el-table-column>
          <el-table-column label="收费项名" align="center" prop="chargeitemName">
           
          </el-table-column>
          <el-table-column label="客户姓名" align="center" prop="hirerName" >
      
          </el-table-column>
          
          <el-table-column label="单价" align="center" prop="univalence" >
          
          </el-table-column>
          <el-table-column label="金额" align="center" prop="univalence" >
          
          </el-table-column>
          <el-table-column label="费用开始时间" align="center" prop="startTime" >
          
          </el-table-column>
          <el-table-column label="费用结束时间" align="center" prop="endTime" >
           
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="this.active!==1" style="margin-top: 12px;" @click="back3()">上一步</el-button>
        <el-button v-if="this.active!==2" style="margin-top: 12px;" @click="next3()">下一步</el-button>
        <el-button style="margin-top: 12px;" @click="cancel3">取消</el-button>
        <el-button v-if="active === 2" style="margin-top: 12px;" @click="addListchaobiao">确定</el-button>
      </div>

      <!-- <div slot="footer" class="dialog-footer">
        <el-button style="margin-top: 12px;" @click="cancel">取消</el-button>
        <el-button  style="margin-top: 12px;" @click="cancel">生成数据</el-button>
      </div> -->
    </el-dialog>

  </div>
</template>

<script>
import { listChargedata, getChargedata, delChargedata, addChargedata, updateChargedata,addChargedatas } from "@/api/feeSettings/chargedata";
import { listChargeitem, getChargeitem, delChargeitem, addChargeitem, updateChargeitem } from "@/api/feeSettings/chargeitem";
import { listRentalcontract, getRentalcontract, delRentalcontract, addRentalcontract, updateRentalcontract} from "@/api/feeSettings/rentalcontract";

import { addMeterreadingdatas, } from "@/api/feeSettings/meterreadingdata";
export default {
  name: "Chargedata",
  dicts: ['zhwy_chargedata_state', 'zhwy_chargedata_refundstatus'],
  data() {
    return {
      active: 1,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收费数据表格数据
      chargedataList: [],
      // 弹出层标题
      title: "",

      // 是否显示弹出层
      open: false,
      open1: false,
      open2: false,
      open3: false,
      opentuifei : false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopsId: null,
        chargeitemId: null,
        univalence: null,
        startTime: null,
        endTime: null,
        deadlineTime: null,
        lastData: null,
        thisData: null,
        rate: null,
        wastage: null,
        number: null,
        latefee: null,
        money: null,
        fundsReceived: null,
        paymentState: null,
        refundStatus: null,
        totalRefundAmount: null,
        billNumber: null,
        chargeTime: null,
        payId: null,
        chargeUser: null
      },

      //收费项
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
        // mode: 5,
        type: 0,
        name: null,
      },

      chargeitemList: [],

//   合同数据
      queryParams3: {
        pageNum: 1,
        pageSize: 10,
        shopsId: null,
        type: null,
        hirerId: null,
      },
      rentalcontractList:[],

      // 存放抄表数据
      chaobiaolist:[],
      queryParams4: {
        pageNum: 1,
        pageSize: 10,
        shopsId: null,
        chargeitemId: null,
        univalence: null,
        startTime: null,
        endTime: null,
        deadlineTime: null,
        lastData: null,
        thisData: null,
        rate: null,
        wastage: null,
        number: null,
        latefee: null,
        money: null,
        fundsReceived: null,
        paymentState: null,
        refundStatus: null,
        totalRefundAmount: null,
        billNumber: null,
        chargeTime: null,
        payId: null,
        chargeUser: null
      },



      // 表单参数
      form: {
        shopsId: '', // 商铺编号
        chargeitemName:'',
        chargeitemId:'',
        startTime:'',
        endTime:'',
        univalence:'',
        hirerId:'',
        shops:[],
      },
      hirerId:'',
      hirerName:'',
      chargeitemName:'',
      // 表单校验
      rules: {
      }
    };
  },

  // 算出总金额
  computed: {
    calculateShuliang() {
            const thisData = Number(this.form.thisData);
        const lastData = Number(this.form.lastData);
        const wastage = Number(this.form.wastage);
        
        return thisData - lastData + wastage;
      // return this.form.thisData - this.form.lastData +this.form.wastage;
    },
    calculateJine() {
        return this.form.number * this.form.univalence * this.form.rate;
    }
  },
  // 监听数据变化
  watch: {
    calculateShuliang(newVal) {
      this.form.number = newVal;
    },
    calculateJine(newVal) {
        this.form.money = newVal;
    }
  },


  created() {
    this.getList();
  },

  methods: {
    // 生成抄表数据
       next3() {
        listChargedata(this.queryParams4).then(response => {
        this.chaobiaolist  = response.rows;
      });
        console.log("1234");
        if (this.active++ > 2) this.active = 0;
        
      },

        back3() {
        if (this.active-- < 0) this.active = 2;
      },
      // 生成抄表数据确定按钮
      addListchaobiao(){
        console.log(this.chaobiaolist)
          addMeterreadingdatas(this.chaobiaolist)
            .then((res)=>{
              this.$modal.msgSuccess("新增成功");
              this.open3=false
            })
            .catch((error) => {
              console.error("操作失败:", error);
            });
          
      },



    //批量新增
    addList(){
      addChargedatas(this.form.shops)
        .then((res)=>{
        this.$modal.msgSuccess("新增成功");
        this.open1 = false;
        this.getList();
        this.active = 1;
      })
      .catch((error) => {
        console.error("操作失败:", error);
      });
    },


  // 方法用于拆分商铺编号
    splitShopsId() {
      console.log('商铺编号:', this.form.shopsId);
      const shopsIdArray = this.form.shopsId.split(',').map(item => item.replace(/"/g, ''));
      console.log(shopsIdArray);
      this.form.shops = [];
      // 遍历商铺编号数组，为每个商铺编号创建一行数据
      shopsIdArray.forEach(shopsId => {
        this.kehu2(shopsId)
        console.log("1234")
        console.log(shopsId)
        console.log(this.form.shops);
      });
    },
      // 搜索用户 根据商铺编号自动匹配用户
          kehu2(id){
            listRentalcontract(id).then(response => {
              console.log(response.rows)
              console.log(response.rows[0].hirerName)
              console.log(response.rows[0].shopsCode)
              //查询用户id
              for(let i = 0;i < response.rows.length;i++){
                if(id == response.rows[i].shopsId){
                  this.hirerId = response.rows[i].hirerId;
                  this.hirerName = response.rows[i].hirerName;
                }
              }
            for(let i = 0;i < this.chargeitemList.length;i++){
                if(this.form.chargeitemId == this.chargeitemList[i].id){
                  this.chargeitemName = this.chargeitemList[i].name;
                }
              }
              //把数据放入数组
              this.form.shops.push({ 
                shopsId : id,
                hirerId : this.hirerId,
                hirerName : this.hirerName,
                chargeitemId: this.form.chargeitemId,
                chargeitemName : this.chargeitemName,
                univalence: this.form.univalence,
                startTime: this.form.startTime,
                endTime: this.form.endTime,
                deadlineTime: this.form.deadlineTime,
                paymentState: 0,
                refundStatus: 0,
                lastData: 0,
                thisData: 0,
                rate: 1,
                wastage: 0,
                number: 0,
                money:this.form.univalence,
                });
            });
          },


      next() {
        this.splitShopsId();
        if (this.active++ > 2) this.active = 0;
      },

      back() {
        if (this.active-- < 0) this.active = 2;
      },

      

    // 根据收费项目自动匹配单价
    wathcdanjia(){
    const id = this.form.chargeitemId
    getChargeitem(id).then((response) => {
        console.log(response.data);
        this.form.univalence=response.data.univalence;
        this.form.chargeitemId=response.data.id;

      });
    },
    // 搜索用户 根据商铺编号自动匹配用户
    kehu(){
      listRentalcontract(this.form).then(response => {
        console.log(response.rows)
        console.log(response.rows[0].hirerName)
        console.log(response.rows[0].shopsCode)
        this.hirerName = response.rows[0].hirerName;
        this.form.hirerId = response.rows[0].hirerId;
        this.shopsCode = response.rows[0].shopsCode
      });
    },


    /** 查询收费数据列表 */
    getList() {
      this.loading = true;
      listChargedata(this.queryParams).then(response => {
        this.chargedataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listChargeitem(this.queryParams2).then(response => {
        this.chargeitemList = response.rows;
      });
      listRentalcontract(this.queryParams3).then(response => {
        this.rentalcontractList = response.rows;

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.active=1;
      this.open1 = false;
      this.open2 = false;
      this.open3 = false;
      this.opentuifei = false;
      this.hirerName = '';
      this.reset();
    },


    // 生成抄表数据取消按钮
    cancel3() {
      console.log(this.chargedataList)
      this.queryParams4.chargeitemId='',
      this.queryParams4.startTime='',
      this.queryParams4.endTime='',
      this.open = false;
      this.active=1;
      this.open1 = false;
      this.open2 = false;
      this.open3 = false;
      this.opentuifei = false;
      this.hirerName = '';
      this.chargeitemId = '';
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        shopsId: null,
        chargeitemId: null,
        univalence: null,
        startTime: null,
        endTime: null,
        deadlineTime: null,
        lastData: null,
        thisData: null,
        rate: null,
        wastage: null,
        number: null,
        latefee: null,
        money: null,
        fundsReceived: null,
        paymentState: null,
        refundStatus: null,
        totalRefundAmount: null,
        billNumber: null,
        chargeTime: null,
        payId: null,
        chargeUser: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加收费数据";
    },
    /** 批量新增按钮操作 */
    handleAdd1() {
      this.reset();
      this.open1 = true;
      this.title = "批量新增";
    },
    /** 批量新增按钮操作 */
    handleAdd2() {
      this.reset();
      this.open2 = true;
      this.title = "生成数据(非抄表)";
    },
    /** 生成抄表数据按钮操作 */
    handleAdd3() {
      this.reset();
      this.open3 = true;
      this.title = "自动生成";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getChargedata(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改收费数据";
      });
    },

    /** 退款按钮操作 */
    handleUpdate2(row) {
      this.reset();
      const id = row.id || this.ids
      getChargedata(id).then(response => {
        this.form = response.data;
        this.opentuifei = true;
        this.title = "收费数据退费";
      });
    },
    // 退款提交按钮
    submitFormtuikuan() {
      this.$refs["form"].validate(valid => {
        if (valid) {
                // 获取实收金额和退款总金额
                const fundsReceived = parseFloat(this.form.fundsReceived);
                const totalRefundAmount = parseFloat(this.form.totalRefundAmount);
                // 判断退款总金额与实收金额的关系
                if (totalRefundAmount < fundsReceived) {
                    this.form.refundStatus = 2; // 退款总金额小于实收金额，状态改为2
                } else if (totalRefundAmount === fundsReceived) {
                    this.form.refundStatus = 1; // 退款总金额等于实收金额，状态改为1
                }else if (totalRefundAmount > fundsReceived) {
                    this.$modal.msgError("退款总金额不能大于实收金额");
                    return; // 不执行修改操作
                }
          if (this.form.id != null) {
            
            updateChargedata(this.form).then(response => {
              // this.form.refundStatus = 1;
              this.opentuifei = false;
              this.getList();
              this.$modal.msgSuccess("修改成功");
            });
          }
        }
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateChargedata(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.opentuifei = false;
              this.getList();
            });
          } else {
            addChargedata(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
          
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除收费数据编号为"' + ids + '"的数据项？').then(function() {
        return delChargedata(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('feeSettings/chargedata/export', {
        ...this.queryParams
      }, `chargedata_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
